package com.stox.ui.javafx.navigator.widget;

import java.util.Arrays;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.layout.HBox;

import org.springframework.context.ApplicationEventPublisher;

import com.stox.event.AbstractModelSelectionEvent;
import com.stox.model.AbstractModel;
import com.stox.ui.javafx.core.widget.wrapper.Button;
import com.stox.ui.javafx.core.widget.wrapper.ImageView;
import com.stox.ui.javafx.core.widget.wrapper.TextField;
import com.stox.ui.javafx.core.widget.wrapper.TreeItem;
import com.stox.ui.javafx.core.widget.wrapper.TreeView;
import com.stox.ui.javafx.navigator.model.AbstractModelTreeItem;
import com.stox.util.ApplicationContextUtil;

public class AutoNavigatorPanel extends HBox {

	public AutoNavigatorPanel(TreeView<String> treeView) {
		
		Button autoButton = new Button(new ImageView("presentation.png"), new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent paramT) {
				
			}
		});
		TextField textField = new TextField("2");
		Button previousButton = new Button(new ImageView("up.png"), new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent paramT) {
				TreeItem<String> treeItem = (TreeItem<String>)treeView.getSelectionModel().getSelectedItem();
				if(treeItem == null) treeItem = ((TreeItem<String>)treeView.getRoot()).getLastLeaf();
				navigateTo(treeView,treeItem.previousTreeItem());
			}
		});
		Button nextButton = new Button(new ImageView("down.png"), new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent paramT) {
				TreeItem<String> treeItem = (TreeItem<String>)treeView.getSelectionModel().getSelectedItem();
				if(treeItem == null) treeItem = (TreeItem<String>)treeView.getRoot();
				navigateTo(treeView,treeItem.nextTreeItem());
			}
		});
		Button firstButton = new Button(new ImageView("double-up.png"), new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent paramT) {
				navigateTo(treeView, (TreeItem<String>)treeView.getRoot());
			}
		});
		Button lastButton = new Button(new ImageView("double-down.png"), new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent paramT) {
				navigateTo(treeView, ((TreeItem<String>)treeView.getRoot()).getLastLeaf());
			}
		});
		
		getChildren().addAll(firstButton, previousButton, autoButton, textField, nextButton, lastButton);
	}
	
	private synchronized void navigateTo(TreeView<String> treeView, TreeItem<String> treeItem) {
		int row = treeView.getRow(treeItem);
		treeItem.expand();
		treeView.scrollTo(row);
		treeView.getSelectionModel().clearAndSelect(row);
		treeView.requestFocus();
		ApplicationEventPublisher applicationEventPublisher = ApplicationContextUtil.getApplicationContext().getBean(ApplicationEventPublisher.class);
		AbstractModel abstractModel = ((AbstractModelTreeItem<String>)treeItem).getModel(); 
		applicationEventPublisher.publishEvent(new AbstractModelSelectionEvent<AbstractModel>(Arrays.asList(abstractModel)));
	}

}
